package com.zero.rocketmq.api.delay;

import com.zero.rocketmq.api.utils.RocketMQContents;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 延迟消息
 */
public class DelayProducer {

    public static void main(String[] args) throws MQClientException {

        DefaultMQProducer producer = new DefaultMQProducer(RocketMQContents.PRODUCER_GROUP);

        producer.setNamesrvAddr(RocketMQContents.NAMESERVER_ADDR);

        producer.start();

        try{
            for (int i = 0;i< 10;i++){

                Message msg = new Message(RocketMQContents.TOPIC,
                        "TagA",
                        ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
                );

                // This message will be delivered to consumer 10 seconds later.
                //1 - 18,DelayTimeLevel分别对应
                //messageDelayTime=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h。
                msg.setDelayTimeLevel(3);
                SendResult sendResult = producer.send(msg);
                System.out.printf("%s%n", sendResult);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        producer.shutdown();

    }
}
